package com.cn.server.module.chat.handler.websocketHandler;

import com.cn.common.core.exception.ErrorCodeException;
import com.cn.common.core.model.Result;
import com.cn.common.core.model.ResultCode;
import com.cn.common.module.chat.websocket.ChatHttpRequest;
import com.cn.common.utils.GsonHelperUtils;
import com.cn.server.module.chat.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @Author: jia
 * @Descirption:
 * @Date: 2018/5/14 13:48
 * @Modified By:
 */

@Component
public class WSChatHandlerImpl implements WSChatHandler {
    @Autowired
    private ChatService chatService;
    /**
     * 公共聊天
     * @param playerId
     * @param data 来自浏览器的数据
     * @return
     */
    @Override
    public Result<?> publicChat(long playerId, String data) {
        try {
            //将data格式化
            ChatHttpRequest chatHttpRequest = GsonHelperUtils.fromJson(data, ChatHttpRequest.class);

            //参数校验
            if(StringUtils.isEmpty(chatHttpRequest.getContext())){
                return Result.ERROR(ResultCode.AGRUMENT_ERROR);
            }

            //执行业务
            chatService.publicChat(playerId, chatHttpRequest.getContext());
        } catch (ErrorCodeException e) {
            e.printStackTrace();
            return Result.ERROR(e.getErrorCode());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR(ResultCode.UNKOWN_EXCEPTION);
        }
        return Result.SUCCESS();

    }
}
